import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.creawor.hz_market.t_balance_office.QueryMultiConditions;
import com.creawor.imei.base.Common_qry_QueryMap;
import com.creawor.imei.util.StringUtil;


public class AjaxServlet extends HttpServlet {
	protected final transient Log log = LogFactory.getLog(getClass());
	public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
	{ 
		response.setContentType("text/xml; charset=gbk"); 
		response.setHeader("Cache-Control","no-cache"); 
		response.setHeader("Pragma","no-cache"); 
		response.setDateHeader("Expires",0);  
		PrintWriter out=response.getWriter(); 

		StringBuffer sb=new StringBuffer();  
		 String county1 = request.getParameter("county1");
		 //county1 = new String(county1.getBytes("ISO-8859-1"),"GBK");
		 String county2 = request.getParameter("county2");	
		 //county2 = new String(county2.getBytes("ISO-8859-1"),"GBK");
		 String channelitems = request.getParameter("channelitems");
		 //channelitems = new String(channelitems.getBytes("ISO-8859-1"),"GBK");
		 Map fields=(Map)this.getServletContext().getAttribute("filedMap");
//		 sb.append("hello the world...");
		 log.debug("AjaxServlet doGet....");
		 try {
			Common_qry_QueryMap query_map0 = new Common_qry_QueryMap();
			
			String sql1 = "select * from t_county where county_name like N'"+county1+"%'";
			String sql2 = "select * from t_county where county_name like N'"+county2+"%'";
//			System.out.println("sql1="+sql1);
//			System.out.println("sql2="+sql2);
			ArrayList sl = query_map0.findAll(sql1);
			ArrayList sl2 = query_map0.findAll(sql2);
			if(sl!=null&&sl2!=null)
			{
//				System.out.println("sl2="+sl2);
				Map map1 = (Map)sl.get(0);
				Map map2 = (Map)sl2.get(0);
				sb.append("<div class='ipt' id=popupWin> ");
				sb.append("<table border=0 cellpadding=0 cellspacing=0 style='border-collapse:collapse'><tr><td colspan=2 class=t-01>");
				
				sb.append(county1);
				sb.append("</td><td class=t-02>");
				sb.append(county2);
				sb.append("</td></tr>");
				
				if(channelitems==null||"".equals(channelitems))
				{
					channelitems="population_num:gdp:gdp_average:agpopulation:unagpopulation:key_industry:physiognomy:city_ave_include:county_ave_include:schoolage_percent:permanent_percent:run_percent";
					
				
				}
				//else
				{
					String[]items =channelitems.split(":");
					Map rowmap = (Map) fields.get("t_county");
					for (int i = 0; i < items.length; i++) {
						String item = items[i];
						//System.out.println("item="+item);
						sb.append("<tr><td class=t-03>");
						sb.append(""+rowmap.get(item)==null?"":rowmap.get(item));
						String titles1=StringUtil.valueOf(""+map1.get(item));
						sb.append("</td>")
						.append("<td class=t-04 style='width:180px;' title='").append(titles1).append("'>");
						if(StringUtil.valueOf(""+map1.get(item)).length()>140)
						{
							String channels=StringUtil.valueOf(""+map1.get(item)).substring(0, 140)+"......";
							sb.append(channels);
						}
						else
						{
							sb.append(map1.get((item))==null?"":map1.get((item)));
						}
						//sb.append("&nbsp;</td>");
						//sb.append("<td class=t-04 style='width:180px;'>");
						
						String titles=StringUtil.valueOf(""+map2.get(item));
						sb.append("</td>")
						.append("<td class=t-04 style='width:180px;' title='").append(titles).append("'>");
						if(StringUtil.valueOf(""+map2.get(item)).length()>140)
						{
							String channels=StringUtil.valueOf(""+map2.get(item)).substring(0, 140)+"......";
							sb.append(channels);
						}
						else
						{
							sb.append(map2.get((item))==null?"":map2.get((item)));
						}
						//sb.append(map2.get(item)==null?"":map2.get(item));
						sb.append("&nbsp;</td>");
						sb.append(" </tr>");
					}
				}
			
			sb.append(QueryMultiConditions.getRadioStrs(county1,request));
			sb.append("</table>");
			sb.append("</div>");
			//System.out.println("str="+sb.toString());
			
			}
		 } catch (Exception e) {
			
			e.printStackTrace();
		}
			
			

		
		out.write(sb.toString());  
		out.flush();
		out.close();  
		
	}
	
	
}
